package cn.iocoder.yudao.module.ai.service.creationwork;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.creationwork.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.creationwork.CreationWorkDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.creationwork.CreationWorkMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * AI 创作作品 Service 实现类
 *
 * @author zhaokang
 */
@Service
@Validated
public class CreationWorkServiceImpl implements CreationWorkService {

    @Resource
    private CreationWorkMapper creationWorkMapper;

    @Override
    public Long createCreationWork(CreationWorkSaveReqVO createReqVO) {
        // 插入
        CreationWorkDO creationWork = BeanUtils.toBean(createReqVO, CreationWorkDO.class);
        creationWorkMapper.insert(creationWork);
        // 返回
        return creationWork.getId();
    }

    @Override
    public void updateCreationWork(CreationWorkSaveReqVO updateReqVO) {
        // 校验存在
        validateCreationWorkExists(updateReqVO.getId());
        // 更新
        CreationWorkDO updateObj = BeanUtils.toBean(updateReqVO, CreationWorkDO.class);
        creationWorkMapper.updateById(updateObj);
    }

    @Override
    public void deleteCreationWork(Long id) {
        // 校验存在
        validateCreationWorkExists(id);
        // 删除
        creationWorkMapper.deleteById(id);
    }

    private void validateCreationWorkExists(Long id) {
        if (creationWorkMapper.selectById(id) == null) {
            throw exception(CREATION_WORK_NOT_EXISTS);
        }
    }

    @Override
    public CreationWorkDO getCreationWork(Long id) {
        return creationWorkMapper.selectById(id);
    }

    @Override
    public PageResult<CreationWorkDO> getCreationWorkPage(CreationWorkPageReqVO pageReqVO) {
        return creationWorkMapper.selectPage(pageReqVO);
    }

}